//
//  JSMessageCell.m
//  3 -- QQ project
//
//  Created by Rainy Zou on 15/11/24.
//  Copyright © 2015年 Rainy Zou. All rights reserved.
//

#import "JSMessageCell.h"
#import "JSMessage.h"
#import "MessageFrame.h"
@interface JSMessageCell()

@property (nonatomic,weak) UILabel *timerLabel;
@property (nonatomic, weak) UIButton *textView;
@property (nonatomic, weak) UIImageView *iconView;

@end
@implementation JSMessageCell

- (void)setMessageFrame:(MessageFrame *)messageFrame
{
    _messageFrame = messageFrame;
    JSMessage *message = _messageFrame.message;
    
    
    _timerLabel.text = message.time;
    _timerLabel.frame = _messageFrame.timerF;
    _timerLabel.font = [UIFont systemFontOfSize:15];
    _timerLabel.textAlignment = NSTextAlignmentCenter;
    
    [_textView setTitle:message.text forState:UIControlStateNormal];
    _textView.frame = _messageFrame.textF;
    _textView.titleLabel.font= [UIFont systemFontOfSize:15];
    [_textView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _textView.titleLabel.numberOfLines = 0;
    if (message.type == JSMessageTypeMe) {
        
        [_textView setBackgroundImage:[self resizeImage:@"chat_send_nor.png"] forState:UIControlStateNormal];
        [_textView setBackgroundImage:[self resizeImage:@"chat_send_press_pic.png"] forState:UIControlStateHighlighted];
    }else{
        [_textView setBackgroundImage:[self resizeImage:@"chat_recive_nor.png"] forState:UIControlStateNormal];
        [_textView setBackgroundImage:[self resizeImage:@"chat_recive_press_pic.png"] forState:UIControlStateHighlighted];
    }
    _textView.contentEdgeInsets= UIEdgeInsetsMake(20, 20, 20, 20);
    
    NSString *icon = message.type == JSMessageTypeMe?@"me":@"other";
    [_iconView setImage:[UIImage imageNamed:icon]];
    _iconView.layer.cornerRadius = 25;
    _iconView.layer.masksToBounds = YES;
    _iconView.frame = _messageFrame.iconF;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UILabel *timerLabel = [[UILabel alloc]init];
        [self.contentView addSubview:timerLabel];
        self.timerLabel = timerLabel;
        
        UIButton *textView= [[UIButton alloc]init];
        [self.contentView addSubview:textView];
        self.textView = textView;
        
        UIImageView *imageView = [[UIImageView alloc]init];
        [self.contentView addSubview:imageView];
        self.iconView = imageView;
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (UIImage *)resizeImage:(NSString *)imageName
{
    UIImage *image = [UIImage imageNamed:imageName];
    return [image stretchableImageWithLeftCapWidth:image.size.width/2 topCapHeight:image.size.height/2];
}

@end
